package org.test.redis.properties.parse;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

/**
 * Created by wangyg 
 * on 2017/3/9 17:17
 * 通过properties类获取properties文件中的配置信息的方式
 */
public class UseProperties {
	public static Properties getProperties() throws IOException {
		Properties prop = new Properties();
		InputStream in = getInputStream();
		prop.load(in);
		return prop;
	}

	public static InputStream getInputStream() throws IOException {

		//		1.通过当前类加载器的getResourceAsStream方法获取下载地址

		InputStream in = UseProperties.class.getClassLoader().getResourceAsStream("redis.properties");
		//		2.从文件获取
		InputStream in2 = new FileInputStream(
				new File("D:\\TempFiles\\IntellijIdea\\Pisum\\RedisTest\\src\\main\\resources\\redis.properties"));
		//		3.也是通过类加载器来获取，和第一种一样

		InputStream in3 = ClassLoader.getSystemResourceAsStream("redis.properties");
		//		4.在servlet中，还可以通过context来获取InputStream

		//		InputStream in4 = context.getResourceAsStream("filePath");
		//		5.通过URL来获取

		//		URL url = new URL("redis.properties");
		//		InputStream inStream = url.openStream();
		return in;
	}

	public static void main(String[] args) throws IOException {
		String property = getProperties().getProperty("redis.ip");
		System.out.println(property);
	}
}
